package com.syni.mdd.yhd.common.all.service;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsUserBusinessFocusDao;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessGroupMessage;
import com.syni.mdd.yhd.common.all.entity.BmsUserBusinessFocus;
import com.syni.mdd.yhd.common.all.entity.BmsVideo;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.*;

/**
 * @className BmsUserOperationLogService
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsUserBusinessFocusService extends BaseService<BmsUserBusinessFocus, Integer> {

    @Autowired
    BmsUserBusinessFocusDao bmsUserBusinessFocusDao;

    @Autowired
    BmsVideoService bmsVideoService;

    @Override
    public BmsUserBusinessFocusDao getBaseDao() {
        return bmsUserBusinessFocusDao;
    }


    public List<BmsUserBusinessFocus> findUserFouce(Integer userId, Pageable pageable) {
        return bmsUserBusinessFocusDao.findByBmsUserId(userId,pageable);
    }

    public List<BmsUserBusinessFocus> findUserFouce(Integer userId) {
        return bmsUserBusinessFocusDao.findByBmsUserId(userId);
    }

    public BmsUserBusinessFocus findUserFouce(Integer userId,Integer businessId) {
        return bmsUserBusinessFocusDao.findByBmsUserIdAndBmsBusinessId(userId,businessId);
    }

    public boolean checkUserIsFocusBusiness(Integer userId,Integer businessId){
        boolean ret = false;
        BmsUserBusinessFocus bmsUserBusinessFocus = bmsUserBusinessFocusDao.
                findByBmsUserIdAndBmsBusinessId(userId,businessId);
        if(bmsUserBusinessFocus != null){
            return true;
        }
        return ret;
    }

    public List<BmsUserBusinessFocus> getBusinessFoces(Integer businessId){
        return bmsUserBusinessFocusDao.findByBmsBusinessId(businessId);
    }

    /**
     * @Description 获取我关注的商家视频
     * @Author: cfx
     * @Date: 2019/7/8
     */
    public Map<String, Object> getMyFocusVideo(Integer userId, Integer pageNum, Integer pageSize) {
        //找出用户关注的商家
        List<BmsVideo> bmsVideos = new LinkedList<>();
        List<BmsUserBusinessFocus> bmsUserBusinessFocuses = bmsUserBusinessFocusDao.findByBmsUserId(userId);
        Set<Integer> businessIds = new HashSet<>();
        if(bmsUserBusinessFocuses != null && bmsUserBusinessFocuses.size() > 0){
            for(BmsUserBusinessFocus bmsUserBusinessFocus : bmsUserBusinessFocuses){
                businessIds.add(bmsUserBusinessFocus.getBmsBusinessId());
            }
            if(businessIds.size() > 0){
                Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize, Sort.Direction.DESC,"newTime");
                Map<String,Object> condition = new HashMap<>();
                condition.put("bmsId_IN",businessIds);
                condition.put("isDelete", Constants.COMMON_FLAG_FALSE_INT);
                condition.put("isOff", Constants.COMMON_FLAG_FALSE_INT);
                condition.put("isRelease", Constants.COMMON_FLAG_FALSE_INT);
                bmsVideos.addAll(bmsVideoService.getBaseDao().findAll(new SpecificationUtils(condition),pageable).getContent());
                Set videoIds = bmsVideoService.getVideoIds(bmsVideos);
                bmsVideoService.findVideoBussess(bmsVideos);
                bmsVideoService.setUserLikeAndFoot(userId.toString(),bmsVideos,videoIds);
                bmsVideoService.setVideoStatic(bmsVideos);
                bmsVideoService.setSendMes(bmsVideos,videoIds);
            }
        }
        return ResultMapHelper.success(bmsVideos);
    }

    public Integer countUserFocsBusinessNum(Integer userId) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("bmsUserId",userId);
        Integer countNum = (int)bmsUserBusinessFocusDao.count(new SpecificationUtils(conditionMap));
        return countNum;
    }

    public void focusBusiness(Integer userId, Integer businessId) {
        if(!checkUserIsFocusBusiness(userId,businessId)){
            BmsUserBusinessFocus bmsUserBusinessFocus = new BmsUserBusinessFocus();
            bmsUserBusinessFocus.setBmsUserId(userId);
            bmsUserBusinessFocus.setBmsBusinessId(businessId);
            bmsUserBusinessFocus.setNewTime(new Date());
            bmsUserBusinessFocusDao.save(bmsUserBusinessFocus);
        }
    }

    public Map<String, Object> getMyBusinessFoucsNum(Integer businessId) {
        Map<String,Object> conditionMap = new LinkedHashMap<>();
        conditionMap.put("bmsBusinessId",businessId);
        conditionMap.put("isClick",Constants.COMMON_FLAG_FALSE_INT);
        long size = bmsUserBusinessFocusDao.count(new SpecificationUtils(conditionMap));
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("newFocusNum",size);
        return ResultMapHelper.success(resultMap, ResultMsg.MSG_GET_SUCCESS);
    }

    public Map<Integer,BmsUserBusinessFocus> getMapByUserIdsAndBusinessId(List<String> sendUsers,Integer businessId) {
        Map<Integer,BmsUserBusinessFocus> bmsUserBusinessFocusMap = new HashedMap();
        Set<Integer> userIds = new HashSet<>();
        if(sendUsers.size() > 0){
            for(String user : sendUsers){
                userIds.add(Integer.parseInt(user));
            }
        }
        if(userIds.size() > 0){
            Map<String,Object> conditionMap = new HashMap();
            conditionMap.put("bmsUserId_IN",userIds);
            conditionMap.put("bmsBusinessId",businessId);
            List<BmsUserBusinessFocus> businessFocusList = bmsUserBusinessFocusDao.findAll(new SpecificationUtils(conditionMap));
            if(businessFocusList != null && businessFocusList.size() > 0){
                for(BmsUserBusinessFocus bmsUserBusinessFocus : businessFocusList){
                    bmsUserBusinessFocusMap.put(bmsUserBusinessFocus.getBmsUserId(),bmsUserBusinessFocus);
                }
            }
        }
        return bmsUserBusinessFocusMap;
    }
}
